package com.airforce.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class HttpUtil {


        public String doPost(String url, Map<String, String> map, String charset,String ip,String port) {
            HttpClient httpClient = null;
            HttpPost httpPost = null;
            String result = null;
            StringBuilder sb = new StringBuilder();
            try {
                httpClient = new SSLClient();
                httpClient.getParams().setParameter(
                        ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2965);
                httpPost = new HttpPost(url);
                httpPost.getParams().setParameter(
                        ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);

                // 设置参数
                List<NameValuePair> list = new ArrayList<NameValuePair>();
                Iterator iterator = map.entrySet().iterator();
                while (iterator.hasNext()) {
                    Entry<String, String> elem = (Entry<String, String>) iterator
                            .next();
                    list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
                }
                if (list.size() > 0) {
                    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,
                            charset);
                    httpPost.setEntity(entity);
                }
                //IP代理
                HttpHost proxy=new HttpHost(ip, Integer.parseInt(port));
                RequestConfig requestConfig= RequestConfig.custom().setProxy(proxy).build();
                httpPost.setConfig(requestConfig);

                HttpResponse response = httpClient.execute(httpPost);  //执行请求
                //获取Cookie

                List<Cookie> cookies = ((AbstractHttpClient)httpClient).getCookieStore().getCookies();
                for(Cookie cookie: cookies)
                    sb.append(cookie.getName() + "=" + cookie.getValue() + ";");


            }

            catch (Exception ex) {
                ex.printStackTrace();
            }


            return sb.toString();
        }



    public String doGet(String url, String charset) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = null;
        String result = null;
        try {
            httpClient = new SSLClient();

            //设置Cooike策略
            httpClient.getParams().setParameter(
                    ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2965);
            httpGet=new HttpGet(url);
            httpGet.getParams().setParameter(

                    ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);

            //添加请求头
//            httpGet.setHeader("Cookie",Cookie);
            httpGet.setHeader("Authorization","Bearer 1ca4988c-2002-4c5a-8d6b-882f39e31d1f");
            //执行请求
            HttpResponse response = httpClient.execute(httpGet);


                if (response != null) {

                    HttpEntity resEntity = response.getEntity();
                    if (resEntity != null) {
                        result = EntityUtils.toString(resEntity, charset);
                    }
                }
        } catch (Exception ex) {
            ex.printStackTrace();
        }


        return result;
    }

    public static void main(String args[]){
            HttpUtil client=new HttpUtil();
           String res= client.doGet("http://localhost:8080/index/app/awardall","UTF-8");
        System.out.println(res);

    }






}
